<!-- 登录注册页面 -->
<template>
	<view class="login-page">
		<img class="back-img" src="./images/back.png" alt="">
		<view class="content">
			<view class="image-view">
				<img class="logo-img" src="./images/logo.png" alt="">
				<img class="title-img" src="./images/title.png" alt="">

			</view>
			<view class="buttom-view" @click="loginIn">微信登录</view>
		</view>
		<view class="agreement">
			<text>隐私协议、用户协议</text>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import apis from '@/apis.js'
	export default {
		data() {
			return {}
		},
		computed: {
			...mapState(['TOKEN', 'UID']),
			//是否需要登录
			isLogin() {
				return this.TOKEN
			}
		},
		onLoad() {

		},
		onShow() {
			if (this.isLogin) {
				uni.redirectTo({
					url: '/pages/tabBar/homePage/index'
				})
			}
		},
		methods: {
			...mapMutations(['setToken', 'setUid']),
			//获取登录 wxloing code
			getCode() {
				return new Promise((reslove, reject) => {
					uni.login({
						provider: 'weixin',
						success: (loginRes) => {
							if (loginRes.errMsg.indexOf('ok') > -1) {
								reslove(loginRes.code)
								return
							}
							console.log('获取信息失败！', loginRes)
							reject(null)
						}
					})
				})
			},
			loginIn() {
				
				this.getCode().then(code => {
					console.log('获取到登录code', code)
					this.$http.post(apis.wechatLogin, {
						code,
					}).then(res => {
						if (res.data.code === 0) {
							this.setToken(res.data.data.token)
							this.setUid(res.data.data.uid)
							uni.redirectTo({
								url: '/pages/tabBar/homePage/index'
							})
						}
					})
				})
			}
		},
	}
</script>

<style lang="scss" scoped>
	.login-page {
		height: 100vh;

		.back-img {
			position: fixed;
			left: 0;
			top: 0;
			width: 100vw;
			height: 100vh;
			z-index: -1;
		}

		.agreement {
			position: fixed;
			width: 100%;
			text-align: center;
			bottom: 24rpx;

			text {
				font-size: 24rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #323232;
				line-height: 33rpx;
			}
		}

		.content {
			box-sizing: border-box;
			height: 100vh;
			z-index: 1;
			padding: 24rpx 35rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: space-between;

			.image-view {
				margin-top: 290rpx;
				display: flex;
				flex-direction: column;
				align-items: center;

				.logo-img {
					width: 150rpx;
					height: 150rpx;
				}

				.title-img {
					width: 179rpx;
					height: 54rpx;
					margin-top: 40rpx;
				}
			}

			.buttom-view {
				width: 100%;
				font-size: 28rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #FFFFFF;
				line-height: 40rpx;
				letter-spacing: 2rpx;
				background: linear-gradient(135deg, #E42F36 0%, #F44B25 100%);
				border-radius: 44rpx;
				text-align: center;
				height: 88rpx;
				line-height: 88rpx;
				margin-bottom: 153rpx;
			}

			// .buttom-view:active {
			// 	opacity: 0.6;
			// }
		}
	}
</style>
